// pages/class/index.js
var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        curNav: 1,
        curIndex: 0,
        cart: [],
        cartTotal: 0,
        navList: [],
        dishesList: [],
        service_url: '',
        height: '',
        pageNo: 1,
        pageSize: 13,
        count: 0,
        searchText: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

        var _this = this;
        var serverUrl = app.data.server;
        wx.getSystemInfo({
            success: (res) => {
                this.setData({
                    height: res.windowHeight,
                    service_url: serverUrl
                })
            }
        })
        app.paiRequest("/app/course/tcourses/class", { pageNo: 1, pageSize: this.data.pageSize }).then(function (res) {
            var data = res.data;
            var dishesList = _this.data.dishesList;
            dishesList = data.course.list;
            _this.setData({
                navList: data.items,
                dishesList: dishesList,
                count: data.course.count,
                curNav: data.items[0].dictValue
            })
        })


    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },


    /**
     * 分类点击事件
     */
    selectNav: function (event) {
        console.log(event);
        let id = event.target.dataset.id, index = parseInt(event.target.dataset.index);
        self = this;
        let _this = this;
        app.paiRequest("/app/course/tcourses/findPage", { parentCode: 0, courseItem: id, pageNo: 1, pageSize: this.data.pageSize }).then(function (res) {
            var data = res.data;
            console.log(res)
            _this.setData({
                dishesList: data.list,
                count: data.count,
            })
        })

        this.setData({
            curNav: id,
            curIndex: index
        })
    },
    /**
     * 数据
     */
    selectDish: function (event) {
        let id = event.currentTarget.dataset.dish;
        wx.navigateTo({
            url: '../detail/index?id=' + id,
        })
    },
    inputData: function (e) {
        this.setData({
            searchText: e.detail.value
        })
    },
    tapSearch: function () {
        var _this = this;
        app.paiRequest("/app/course/tcourses/findPage", { courseTitle: this.data.searchText, parentCode: 0, courseItem: _this.data.curNav, pageNo: 1, pageSize: this.data.pageSize }).then(function (res) {
            var data = res.data;
            _this.setData({
                dishesList: data.list,
                count: data.count,
            })
        })
    },
    onScrollLower: function () {
        let _this = this;
        var count = this.data.count;
        var dishesList = _this.data.dishesList;
        var lent = dishesList.length;
        if (count == lent) {
            wx.showToast({
                title: '到底了',
                icon: 'success',
                duration: 1000
            })
        } else {
            var pageNo = this.data.pageNo;
            app.paiRequest("/app/course/tcourses/findPage", { courseTitle: this.data.searchText, parentCode: 0, courseItem: _this.data.curNav, pageNo: pageNo + 1, pageSize: this.data.pageSize }).then(function (res) {
                var data = res.data;
                var cont = dishesList.concat(data.list);
                _this.setData({
                    dishesList: cont,
                    count: data.count,
                    pageNo: pageNo + 1
                })
            })
        }
    }
})